package front;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;

import be.db_backend;

import mt.middle;

public class SubForum {
	private int subForumID;
	private String name;
 	private int numberOfThreads;
 	private Timestamp createdOn;
	private int minimumlvl;
	private front.Thread latestThread;
	private front.Thread[] threads;
	private middle m;
	
	public SubForum(){
		this.m = new middle(new db_backend());
	}
	
 	public SubForum(int subForumID, String name, int numberOfThreads, Timestamp createdOn, int minimumRestrictionlvl) {
		this.subForumID = subForumID;
		this.name = name;
		this.numberOfThreads = numberOfThreads;
		this.createdOn = createdOn;
		this.minimumlvl = minimumRestrictionlvl;
		this.m = new middle(new db_backend());
	}
 	
 	public SubForum(int subForumID, String name, int minimumRestrictionlvl) {
		this.subForumID = subForumID;
		this.name = name;
		this.numberOfThreads = 0;
		this.createdOn = new Timestamp(System.currentTimeMillis());
		this.minimumlvl = minimumRestrictionlvl;
		this.m = new middle(new db_backend());
	}
 	
 	public synchronized void Create() {
 		try {
			int id = m.CreateSubForum(subForumID, name, numberOfThreads, createdOn, minimumlvl);
			this.subForumID = id;
		} catch (Exception e) {
			System.out.println("FAIL! INSERT!");
			e.printStackTrace();
		}
	}
 	
 	public synchronized void Edit(int subForumID, String name, int numberOfThreads, Date createdOn, int minimumRestrictionlvl) {
	}
 	
 	public synchronized void Delete(){
 		try {
			m.DeleteSubForum(subForumID);
		} catch (Exception e) {
			System.out.println("FAIL! DELETE!");
			e.printStackTrace();
		}
 	}
 	
 	public synchronized void incrementNumberOfThreads(){
 		try{
			m.incrementNumberOfThreads(subForumID);
			numberOfThreads++;
		}
		catch(Exception e){
			System.out.println("SQL statement error!");
			e.printStackTrace();
		}
 	}

 	public ArrayList<front.Thread> getAllThreads(){
		return m.getAllThreads(subForumID);
 	}
 	
	public int getSubForumID() {
		return subForumID;
	}

	public void setSubForumID(int subForumID) {
		this.subForumID = subForumID;
	}

	public String getName() {
		if(name == null || name == "")
			name = m.getSubForumName(subForumID);
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getNumberOfThreads() {
		return numberOfThreads;
	}

	public void setNumberOfThreads(int numberOfThreads) {
		m.setNumberOfThreadsOnASubForum(subForumID, numberOfThreads);
		this.numberOfThreads = numberOfThreads;
	}

	public Date getCreatedOn() {
		return createdOn;
	}
	public void setCreatedOn(Timestamp createdOn) {
		this.createdOn = createdOn;
	}

	public int getMinimumlvl() {
		return minimumlvl;
	}

	public void setMinimumlvl(int minimumlvl) {
		this.minimumlvl = minimumlvl;
	}
	
	public front.Thread getLatestThread() {
		if(latestThread == null)
			this.latestThread = m.getLatestThread(subForumID);
		return this.latestThread;
	}

	public void setLatestThread(front.Thread latestThread) {
		this.latestThread = latestThread;
	}

	public front.Thread[] getThreads() {
		if(threads == null){
			ArrayList<front.Thread> result = m.getAllThreads(subForumID);
			front.Thread[] ret = new front.Thread[result.size()];
		
			int i = 0;
			for(front.Thread t : result){
				ret[i] = t;
				i++;
			}
			this.threads = ret;
			return ret;
		}
		else{
			return this.threads;
		}
	}

	public void setThreads(front.Thread[] threads) {
		this.threads = threads;
	}
}
